<?php
namespace app\teacher\command;

use app\common\model\ClassesSignLog;
use app\common\model\ClassesSign;
use app\common\model\UserBadge;
use think\Cache;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class FullSignIn extends Command
{
    protected function configure()
    {
        $this->setName('fullSignIn')->setDescription('检测是否全勤，不全勤，取消全勤勋章');
    }

    protected function execute(Input $input, Output $output)
    {
        $isDeal = Cache::get('deal_quanqin_xuanzhang');
        if ($isDeal==1) {
            $output->info('数据处理中');
            return;
        }
        Cache::set('deal_quanqin_xuanzhang', 1);
        $model = new ClassesSign();
        $logModel = new ClassesSignLog();
        $signIds = $model->where('sign_end_time', '<', time())->column('id');
        $signLogs = $logModel->whereIn('sign_id', $signIds)->where('status', 1)->group('classes_id,uid')->field('uid, classes_id')->select();
        $userBadgeModel = new UserBadge();
        foreach ($signLogs as $signLog) {
            $userBadgeModel->where('type', 1)->where('user_id', $signLog['uid'])->where('classes_id', $signLog['classes_id'])->update(['state' => 0]);
        }
        Cache::set('deal_quanqin_xuanzhang', 0);
        $output->info(date('Y-m-d H:i:s').'数据处理完成');
        /*$signList =$model->field('count(1) as count, classes_id')->where('id', 'in', $signIds)->group('classes_id')->select();
        foreach ($signList as $sign) {
            $signUser = $logModel->where('classes_id', $sign['classes_id'])->group('uid')->column('uid');
            $signLogList = $logModel->field('count(1) as count, uid')->where('sign_id', 'in', $signIds)->where('uid', 'in', $signUser)->where('status', 2)->where('classes_id', $sign['classes_id'])->group('uid')->select();
            $signLogList = collection($signLogList)->toArray();
            $signLogList = array_combine(array_column($signLogList, 'uid'), array_column($signLogList, 'count'));
            $signArr = array_map(function($user) use ($signLogList) {
                if (isset($signLogList[$user])) return ['uid'=>$user,'count'=>$signLogList[$user]];
                return ['uid'=>$user, 'count'=>0];
            }, $signUser);
            foreach ($signArr as $v) {

            }
        }*/
    }
}